﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Redis;
using TjZheng.Power.RedisEntity;
using TjZheng.Power.DataEntity;
using TjZheng.Cache;

namespace TjZheng.CMS.Process.Business
{
    public partial class UserProcess
    {
        /// <summary>
        /// 删除用户组下的用户权限
        /// </summary>
        /// <param name="objPowerIds"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public InvokeResult DeleteUserPowers(List<long> objPowerIds, long groupId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objPowerIds.IsNull())
            {
                objInvokeResult.ResultCode = "PowerIdsNull";
                return objInvokeResult;
            }
            objPowerIds.Remove(1);//不删除根节点
            if (objPowerIds.IsNull())
            {
                return objInvokeResult;
            }
            BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
            var objUserIds = objBizUserRoleInfo.GetFieldList<long>("GroupId=" + groupId, "UserId");
            if (objUserIds.IsNull())
            {
                objInvokeResult.ResultCode = "UserIdsNull";
                return objInvokeResult;
            }
            BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
            objBizUserPowerInfo.ExecuteTransation(() =>
            {
                objBizUserPowerInfo.Delete(string.Format("UserId in({0}) AND PowerId in({1})", objUserIds.JoinComma(), objPowerIds.JoinComma()));

                if (IsCheckUserPowerRedis)
                {
                    //更新Redis缓存
                    UserPower objUserPower = null;
                    foreach (var objUserId in objUserIds)
                    {
                        objUserPower = new UserPower
                        {
                            UserId = objUserId,
                            PowerIds = objBizUserPowerInfo.GetFieldList<long>("UserId=" + objUserId, "PowerId")
                        };
                        RedisHelper.Store<UserPower>(objUserPower, PowerVariable.ProviderName, PowerVariable.Db);
                    }
                }
            });
            return objInvokeResult;
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="objPowerIds"></param>
        /// <returns></returns>
        public InvokeResult DeletePowers(List<long> objPowerIds)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objPowerIds.IsNull())
            {
                objInvokeResult.ResultCode = "PowerIdsNull";
                return objInvokeResult;
            }
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            objBizPowerInfo.ExecuteTransation(() =>
            {
                BizGroupPowerInfo objBizGroupPowerInfo = new BizGroupPowerInfo();
                var objPowerInfoList = objBizPowerInfo.GetList(string.Format("Id in({0})", objPowerIds.JoinComma()));
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                BizUserPowerInfo objBizUserPowerDependentInfo = new BizUserPowerInfo("Dependent");
                List<long> objDeletePowerIds = null;
                string deletePowerIds = string.Empty;
                foreach (var objPowerInfo in objPowerInfoList)
                {
                    objDeletePowerIds = objBizPowerInfo.GetFieldList<long>(string.Format("IDPath like '{0}%'", objPowerInfo.IDPath), "Id");
                    deletePowerIds = objDeletePowerIds.JoinComma();
                    objBizGroupPowerInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                    objBizUserPowerInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                    objBizUserPowerDependentInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                    objBizPowerInfo.Delete(string.Format("IDPath like '{0}%'", objPowerInfo.IDPath));

                    if (IsCheckUserPowerRedis)
                    {
                        //删除Redis缓存
                        objBizPowerInfo.DeleteByIdsRedis(objDeletePowerIds, PowerVariable.ProviderName, PowerVariable.Db);
                        //todo：看Redis性能，性能差删除组、组部分、独立权限缓存
                    }
                }
            });
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="powerId"></param>
        /// <returns></returns>
        public InvokeResult DeletePower(long powerId)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            var objPowerInfo = objBizPowerInfo.GetRecord(powerId);
            if (objPowerInfo == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                return objInvokeResult;
            }
            var objDeletePowerIds = objBizPowerInfo.GetFieldList<long>(string.Format("IDPath like '{0}%'", objPowerInfo.IDPath), "Id");
            objBizPowerInfo.ExecuteTransation(() =>
            {
                string deletePowerIds = objDeletePowerIds.JoinComma();
                BizGroupPowerInfo objBizGroupPowerInfo = new BizGroupPowerInfo();
                objBizGroupPowerInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                objBizUserPowerInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                BizUserPowerInfo objBizUserPowerDependentInfo = new BizUserPowerInfo("Dependent");
                objBizUserPowerDependentInfo.Delete(string.Format("PowerId in({0})", deletePowerIds));
                objBizPowerInfo.Delete(string.Format("IDPath like '{0}%'", objPowerInfo.IDPath));

                if (IsCheckUserPowerRedis)
                {
                    //删除Redis缓存
                    objBizPowerInfo.DeleteByIdsRedis(objBizPowerInfo.GetFieldList<long>(string.Format("IDPath like '{0}%'", objPowerInfo.IDPath), "Id"), PowerVariable.ProviderName, PowerVariable.Db);
                    //todo：看Redis性能，性能差删除组、组部分、独立权限缓存
                }
            });
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="objUserIds"></param>
        /// <returns></returns>
        public InvokeResult DeleteUsers(List<long> objUserIds)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objUserIds.IsNull())
            {
                objInvokeResult.ResultCode = "UserIdsNull";
                return objInvokeResult;
            }
            BizUserInfo objBizUserInfo = new BizUserInfo();
            objBizUserInfo.ExecuteTransation(() =>
            {
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                objBizUserPowerInfo.Delete(string.Format("UserId in({0})", objUserIds.JoinComma()));
                BizUserPowerInfo objBizUserPowerDependentInfo = new BizUserPowerInfo("Dependent");
                objBizUserPowerDependentInfo.Delete(string.Format("UserId in({0})", objUserIds.JoinComma()));
                BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
                objBizUserRoleInfo.Delete(string.Format("UserId in({0})", objUserIds.JoinComma()));
                objBizUserInfo.DeleteIDString(objUserIds);

                if (IsCheckUserPowerRedis)
                {
                    //删除Redis缓存
                    RedisHelper.DeleteByIds<UserPower>(objUserIds, PowerVariable.ProviderName, PowerVariable.Db);
                    RedisHelper.DeleteByIds<UserPower>(objUserIds, PowerVariable.ProviderName, PowerVariable.Db, "Dependent");
                    RedisHelper.DeleteByIds<UserInfo>(objUserIds, PowerVariable.ProviderName, PowerVariable.Db);
                    RedisHelper.DeleteByIds<UserRole>(objUserIds, PowerVariable.ProviderName, PowerVariable.Db);
                }
                else
                {
                    foreach (var objUserId in objUserIds)
                    {
                        objBizUserInfo.DeleteIDCache(objUserId);
                    }
                }
            });
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public InvokeResult DeleteUser(long userId)
        {
            BizUserInfo objBizUserInfo = new BizUserInfo();
            objBizUserInfo.ExecuteTransation(() =>
            {
                BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
                objBizUserRoleInfo.Delete("UserId=" + userId);
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                objBizUserPowerInfo.Delete("UserId=" + userId);
                objBizUserInfo.Delete(userId);

                if (IsCheckUserPowerRedis)
                {
                    //删除Redis缓存
                    RedisHelper.DeleteById<UserPower>(userId, PowerVariable.ProviderName, PowerVariable.Db);
                    RedisHelper.DeleteById<UserPower>(userId, PowerVariable.ProviderName, PowerVariable.Db, "Dependent");
                    RedisHelper.DeleteById<UserInfo>(userId, PowerVariable.ProviderName, PowerVariable.Db);
                    RedisHelper.DeleteById<UserRole>(userId, PowerVariable.ProviderName, PowerVariable.Db);
                }
                else
                {
                    objBizUserInfo.DeleteIDCache(userId);
                }
            });
            InvokeResult objInvokeResult = new InvokeResult();
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <param name="objGroupIds"></param>
        /// <returns></returns>
        public InvokeResult DeleteGroups(List<long> objGroupIds)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objGroupIds.IsNull())
            {
                objInvokeResult.ResultCode = "GroupIdsNull";
                return objInvokeResult;
            }
            BizGroupInfo objBizGroupInfo = new BizGroupInfo();
            objBizGroupInfo.ExecuteTransation(() =>
            {
                BizGroupPowerInfo objBizGroupPowerInfo = new BizGroupPowerInfo();
                var objPowerIds = objBizGroupPowerInfo.GetFieldList<long>(string.Format("GroupId in({0})", objGroupIds.JoinComma()), "PowerId");
                objBizGroupPowerInfo.Delete(string.Format("GroupId in({0})", objGroupIds.JoinComma()));
                BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
                var objUserIds = objBizUserRoleInfo.GetFieldList<long>(string.Format("GroupId in({0})", objGroupIds.JoinComma()), "UserId");
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                objBizUserPowerInfo.Delete(string.Format("UserId in({0}) And PowerId in({1})", objUserIds.JoinComma(), objPowerIds.JoinComma()));
                objBizUserRoleInfo.Delete(string.Format("GroupId in({0})", objGroupIds.JoinComma()));
                objBizGroupInfo.DeleteIDString(objGroupIds);

                if (IsCheckUserPowerRedis)
                {
                    //更新Redis缓存
                    UserPower objUserPower = null;
                    foreach (var objUserId in objUserIds)
                    {
                        objUserPower = new UserPower
                        {
                            UserId = objUserId,
                            PowerIds = objBizUserPowerInfo.GetFieldList<long>("UserId=" + objUserId, "PowerId")
                        };
                        RedisHelper.Store<UserPower>(objUserPower, PowerVariable.ProviderName, PowerVariable.Db);
                    }
                    RedisHelper.DeleteByIds<GroupInfo>(objGroupIds, PowerVariable.ProviderName, PowerVariable.Db);
                }

            });
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public InvokeResult DeleteGroup(long groupId)
        {
            BizGroupInfo objBizGroupInfo = new BizGroupInfo();
            objBizGroupInfo.ExecuteTransation(() =>
            {
                BizGroupPowerInfo objBizGroupPowerInfo = new BizGroupPowerInfo();
                var objPowerIds = objBizGroupPowerInfo.GetFieldList<long>("GroupId=" + groupId, "PowerId");
                objBizGroupPowerInfo.Delete("GroupId=" + groupId);
                BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
                var objUserIds = objBizUserRoleInfo.GetFieldList<long>("GroupId=" + groupId, "UserId");
                BizUserPowerInfo objBizUserPowerInfo = new BizUserPowerInfo();
                objBizUserPowerInfo.Delete(string.Format("UserId in({0}) And PowerId in({1})", objUserIds.JoinComma(), objPowerIds.JoinComma()));
                objBizUserRoleInfo.Delete("GroupId=" + groupId);
                objBizGroupInfo.Delete(groupId);

                if (IsCheckUserPowerRedis)
                {
                    //更新Redis缓存
                    UserPower objUserPower = null;
                    foreach (var objUserId in objUserIds)
                    {
                        objUserPower = new UserPower
                        {
                            UserId = objUserId,
                            PowerIds = objBizUserPowerInfo.GetFieldList<long>("UserId=" + objUserId, "PowerId")
                        };
                        RedisHelper.Store<UserPower>(objUserPower, PowerVariable.ProviderName, PowerVariable.Db);
                    }
                    RedisHelper.DeleteById<GroupInfo>(groupId, PowerVariable.ProviderName, PowerVariable.Db);
                }
            });
            InvokeResult objInvokeResult = new InvokeResult();
            objInvokeResult.ResultMessage = "删除成功";
            return objInvokeResult;
        }
    }
}
